package com.palfund.threadpool;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 线程池中核心线程、缓存队列、非核心线程添加任务的顺序:核心线程--->缓存队列--->非核心线程
 */
public class MainActivity extends AppCompatActivity {
    static ThreadPoolExecutor executor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //核心线程数,除非allowCoreThreadTimeOut被设置为true,否则它闲着也不会死
        int corePoolSize = 3;
        //设置线程池最大能接受多少线程
        int maxiPoolSize = 5;
        //线程保活时间
        long keepActiveTime = 2;
        //设置时间单位，秒
        TimeUnit timeUnit = TimeUnit.SECONDS;
        //设置线程池缓存队列的排队策略为FIFO，并且指定缓存队列大小为5
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(5);
        //创建ThreadPoolExecutor线程池对象，并初始化该对象的各种参数
        executor = new ThreadPoolExecutor(corePoolSize, maxiPoolSize, keepActiveTime, timeUnit,
                workQueue);
        // 核心线程空闲,也不销毁
        //executor.allowCoreThreadTimeOut(false);
        //往线程池中循环提交线程
        for (int i = 0; i < 4; i++) {
            //创建线程类对象
            Task task = new Task(i);
            //开启线程
            //executor.execute(task);
            //获取线程池中线程的相应参数
            Log.i("MainActivity", "---onCreate()--->" + "线程池中线程数目：" + executor.getPoolSize() +
                    "，队列中等待执行的任务数目：" + executor.getQueue().size() + "，已执行完的任务数目：" + executor
                    .getCompletedTaskCount());
        }
        //待线程池以及缓存队列中所有的线程任务完成后关闭线程池。
        //executor.shutdown();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(Menu.NONE, 1, 1, "Jump2Thread");
        menu.add(Menu.NONE, 2, 1, "Jump2Extend");
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 1:
                startActivity(new Intent(this, ThreadActivity.class));
                break;
            case 2:
                startActivity(new Intent(this, ExtendActivity.class));
                break;
            case 3:

                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    static class Task implements Runnable {
        private int num;

        public Task(int num) {
            this.num = num;
        }

        @Override
        public void run() {
            Log.i("Task", "---run()--->执行" + num + "   线程池中线程数目：" + executor.getPoolSize() +
                    "，队列中等待执行的任务数目：" + executor.getQueue().size() + "，已执行完的任务数目：" + executor
                    .getCompletedTaskCount());
            SystemClock.sleep(5000);
            Log.i("Task", "---run()--->" + num + "执行完毕");
        }
    }
}
